package org.geometerplus.fbreader.library;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.htfyun.utils.DebugLog;

import org.fbreader.util.Pair;
import org.geometerplus.fbreader.Paths;
import org.geometerplus.fbreader.book.IBookCollection;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.resources.ZLResource;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sst on 2017/11/24.
 */

public abstract class BookFileFirstLevelTree extends FirstLevelTree {


    private final Context mContext;
    public BookFileFirstLevelTree(Context context, IBookCollection collection, org.geometerplus.fbreader.formats.PluginCollection pluginCollection) {
        super(collection, pluginCollection, "songBookFile");
        mContext = context;
    }

    @Override
    public Pair<String,String> getTreeTitle() {
        return new Pair(getName(), null);
    }

    
    //search
    @Override
    public String getOpeningStatusMessage() {
        return "search";
    }

    @Override
    public Status getOpeningStatus() {
        return Status.ALWAYS_RELOAD_BEFORE_OPENING;
    }


    @Override
    public void waitForOpening() {
        clear();

        for (String path : queryFilesPath()) {
            addChild(path);
        }
    }

    private void addChild(String path) {
        final File bookFile = new File(path);
        if (!bookFile.exists() || bookFile.isDirectory()) {
            return;
        }

        final ZLFile zlFile = ZLFile.createFileByPath(path);
        if (zlFile != null) {
            new FileTree(this, zlFile, null, null);
        }
    }

    public abstract List<String> queryFilesPath();

    /*
    public static final String SUFFIX_TXT = ".txt";
    public static final String SUFFIX_PDF = ".pdf";
    public static final String SUFFIX_EPUB = ".epub";
    public static final String SUFFIX_ZIP = ".zip";
    public static final String SUFFIX_CHM = ".chm";

    private List<String> queryFilesPath() {

        List<String> booksPath = new ArrayList<>();
        
        String[] projection = new String[]{MediaStore.Files.FileColumns._ID,
                MediaStore.Files.FileColumns.DATA,
                MediaStore.Files.FileColumns.SIZE
        };

        // 查询后缀名为txt与pdf，并且不位于项目缓存中的文档
        Cursor cursor = mContext.getContentResolver().query(
                Uri.parse("content://media/external/file"),
                projection,
                " (" + MediaStore.Files.FileColumns.DATA + " like ? or "
                        + MediaStore.Files.FileColumns.DATA + " like ? or "
                        + MediaStore.Files.FileColumns.DATA + " like ? or "
                        + MediaStore.Files.FileColumns.DATA + " like ? )" 
                        + " and " + MediaStore.Files.FileColumns.SIZE + " > 0", 
                new String[]{
                        "%" + SUFFIX_TXT,
                        "%" + SUFFIX_PDF,
                        "%" + SUFFIX_EPUB,
                        "%" + SUFFIX_CHM}, MediaStore.Files.FileColumns.SIZE + " desc");

        if (cursor != null && cursor.moveToFirst()) {
            int idindex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID);
            int dataindex = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
            int sizeindex = cursor.getColumnIndex(MediaStore.Files.FileColumns.SIZE);


            do {
                String path = cursor.getString(dataindex);

                booksPath.add(path);
            } while (cursor.moveToNext());

            cursor.close();

        }

        return booksPath;
    }
    */
}
